package com.muy.redis.common.tree.actions;

import com.google.common.collect.Lists;
import com.intellij.icons.AllIcons;
import com.muy.redis.common.tree.MTTreeCell;
import com.muy.redis.common.tree.TreePanelMark;
import com.muy.redis.utils.PasswordUtils;
import com.muy.redis.view.window.redis.MrRedisTree;
import com.muy.redis.view.window.redis.bean.MrRedisConnectionParam;
import com.muy.redis.view.window.redis.configuration.MrRedisConfiguration;
import com.muy.redis.view.window.redis.tree.MTTreeCellMrRedisTopDb;

import javax.swing.tree.DefaultMutableTreeNode;

/**
 * @Author jiyanghuang
 * @Date 2023/9/28 22:40
 */
public class MrRedisDuplicateConnectionParamAction extends AbstractMTTreeCellSelectTreeAction {

    private TreePanelMark treePanelMark;

    public MrRedisDuplicateConnectionParamAction(TreePanelMark treePanelMark) {
        super("Duplicate", "Duplicate", AllIcons.Actions.Copy, treePanelMark);
        this.treePanelMark = treePanelMark;
    }

    public MrRedisDuplicateConnectionParamAction(TreePanelMark treePanelMark, String title, String desc) {
        super(title, desc, AllIcons.Actions.Copy, treePanelMark);
    }

    @Override
    public void handleSelectTree(DefaultMutableTreeNode mutableTreeNode, MTTreeCell mtTreeCell) {
        MrRedisTree mrRedisTree = (MrRedisTree) treePanelMark;
        if (mtTreeCell instanceof MTTreeCellMrRedisTopDb) {
            MTTreeCellMrRedisTopDb mtTreeCellMrRedisTopDb = (MTTreeCellMrRedisTopDb) mtTreeCell;
            /**
             * 复制一份连接参数
             */
            MrRedisConnectionParam mrRedisConnectionParam = mtTreeCellMrRedisTopDb.getMrRedisConnectionParam().copyThis();
            MrRedisConfiguration mrRedisConfiguration = MrRedisConfiguration.getInstance(mrRedisTree.getProject());
            mrRedisConfiguration.addData(mrRedisConnectionParam);
            PasswordUtils.savePassword(mrRedisConnectionParam.getId(), mrRedisConnectionParam.getPassword());
            mrRedisTree.buildTree(Lists.newArrayList(mrRedisConnectionParam));
        }
    }
}
